from selenium import webdriver
from time import sleep

'''
常见的api
'''

try:
    driver = webdriver.Chrome()
    driver.get('https://www.baidu.com')
    # 窗口最大化
    driver.maximize_window()
    sleep(2)
    # 窗口最小化
    # driver.minimize_window()
    # 获取窗口尺寸
    print(driver.get_window_size())
    #  清空文本框
    driver.find_element_by_id('kw').send_keys('高考今天开考')
    sleep(1)
    driver.find_element_by_id('kw').clear()
    sleep(1)
    driver.find_element_by_id('kw').send_keys('天和号太空舱')
    driver.find_element_by_id('su').click()
    sleep(1)
    # 点击 天和舱 - 百度百科
    driver.find_element_by_xpath('//*[@id="1"]/h3/a').click()

    # 切换页面
    handles = driver.window_handles
    print('句柄=%s'%handles)
    driver.switch_to.window(handles[1])
    sleep(1)
    # 点击 讨论
    driver.find_element_by_xpath('/html/body/div[3]/div[2]/div/div[1]/dl[1]/dd/a[3]').click()



except Exception as e:
    print(e)
finally:
    sleep(2)
    # 退出浏览器
    driver.quit()